package com.educate.controller;

import com.educate.common.dto.EditCourseDto;
import com.educate.common.lang.Const;
import com.educate.service.EditCourseService;
import com.educate.util.FileUtil;
import com.educate.util.OssFileUpload;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import ws.schild.jave.EncoderException;


import javax.validation.constraints.NotEmpty;


/**
 * @author yjj
 * @date 2021/12/20
 * @Description 编辑课程Controller
 */
@Api(tags = "编辑课程接口")
@RestController
@RequestMapping("/editCourse")
public class EditCourseController {

    @Autowired
    private EditCourseService editCourseService;

    @Autowired
    private OssFileUpload ossFileUpload;

    /**
     * 修改课程基本信息
     * @param editCourseDto
     * @return  String
     */
    @ApiOperation("修改课程基本信息")
    @PostMapping("/updateBasic")
    public String updateCourse(@Validated @RequestBody EditCourseDto editCourseDto){
        editCourseService.updateCourse(editCourseDto);
        return "修改成功！";
    }

    @ApiOperation("修改封面信息")
    @PostMapping("/updateCover")
    public String updateCover(MultipartFile file){
        return Const.OSS_BASE_URL + ossFileUpload.ossUpload(file, Const.COVER_FOLDER);
    }

    @ApiOperation("上传视频文件")
    @PostMapping("/uploadVideo")
    public String uploadVideo(MultipartFile file, @NotEmpty(message = "课程ID不能为空") String courseId) {
        String fileName = ossFileUpload.ossVideoUpload(file, Const.VIDEO_FOLDER);
        int playTime = FileUtil.getVideoDuration(file);
        editCourseService.addCourseVideo(courseId, playTime, fileName);
        return fileName;
    }
}
